from dataclasses import dataclass
from datetime import datetime
from typing import Optional, Literal

@dataclass
class OrderEvent:
    """订单事件"""
    timestamp: datetime
    type: Literal['MARKET', 'LIMIT']  # 订单类型：市价单或限价单
    exchange: str  # 交易所
    symbol: str  # 交易对
    side: Literal['buy', 'sell']  # 买入或卖出
    quantity: float  # 数量
    price: float  # 价格
    stop_loss: Optional[float] = None  # 止损价格
    take_profit: Optional[float] = None  # 止盈价格
    status: str = 'PENDING'  # 订单状态
    order_id: Optional[str] = None  # 订单ID
    
    def __post_init__(self):
        """验证数据"""
        if self.quantity <= 0:
            raise ValueError("数量必须大于0")
        if self.price <= 0:
            raise ValueError("价格必须大于0")
        if self.stop_loss is not None and self.stop_loss <= 0:
            raise ValueError("止损价格必须大于0")
        if self.take_profit is not None and self.take_profit <= 0:
            raise ValueError("止盈价格必须大于0")
